package com.yingxin.yxerp.module.activiti.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yingxin.yx.framework.commons.dto.PageOut;
import com.yingxin.yx.framework.mybatisplus.PageHelper;
import com.yingxin.yxerp.module.activiti.entity.ActivitiForm;
import com.yingxin.yxerp.module.activiti.service.ActivitiProcessDeployService;
import com.yingxin.yxerp.module.activiti.service.IActivitiFormService;
import com.yingxin.yxerp.module.activiti.vo.req.ActivitiFormDeployReqVo;
import com.yingxin.yxerp.module.activiti.vo.req.ActivitiFormQueryVo;
import com.yingxin.yxerp.module.activiti.vo.req.ActivitiFormUpdateVo;
import com.yingxin.yxerp.module.activiti.vo.resp.ActivitiFormRespVo;
import com.yingxin.yxerp.module.easypoi.excel.ExcelExportUtil;
import com.yingxin.yxerp.module.easypoi.excel.entity.ExportParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 表单 前端控制器
 * @author fanchao
 * @since 2021-10-18
 */

@RestController
@RequestMapping("/activiti/form")
public class ActivitiFormController {

    @Autowired
    private IActivitiFormService activitiFormService;

    @Autowired
    private ActivitiProcessDeployService activitiProcessDeployService;

    @PreAuthorize("hasAuthority('bizProcessManager:view')")
    @ApiOperation(value = "查询列表（分页）")
    @PostMapping("/page")
    public PageOut<ActivitiFormRespVo> page(@RequestBody ActivitiFormQueryVo queryVo) {
        IPage<ActivitiFormRespVo> page = activitiFormService.pageVo(PageHelper.in(queryVo), queryVo);
        return PageHelper.out(page);
    }

    @PreAuthorize("hasAuthority('bizProcessManager:view')")
    @ApiOperation(value = "查询列表（全部）")
    @PostMapping("/list")
    public List<ActivitiFormRespVo> list(@RequestBody ActivitiFormQueryVo queryVo) {
        return activitiFormService.listVo(queryVo);
    }

    @PreAuthorize("hasAuthority('bizProcessManager:export')")
    @ApiOperation(value = "导出")
    @PostMapping("/list-export")
    public String listExport( @RequestBody ActivitiFormQueryVo queryVo ) {
        List<ActivitiFormRespVo> list = activitiFormService.listVo( queryVo );
        Workbook workbook = ExcelExportUtil.exportExcel( new ExportParams(), ActivitiFormRespVo.class, list );
        String fileName = ExcelExportUtil.out(workbook, "审批流程.xlsx");
        return fileName;
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public ActivitiFormRespVo get(@PathVariable("id") String id) {
        return activitiFormService.getVo(id);
    }

    @PreAuthorize("hasAuthority('bizProcessManager:deploy')")
    @ApiOperation(value = "部署")
    @PostMapping("/deploy")
    public void deploy(@RequestBody ActivitiFormDeployReqVo reqVo) {
        ActivitiForm activitiForm = activitiFormService.getById(reqVo.getFormId());
        activitiProcessDeployService.deploy(activitiForm, reqVo.getBpmnContent(), reqVo.getBpmnGraphData(),
                reqVo.getTasks(), reqVo.getConditionExpressions());
    }

    @PreAuthorize("hasAuthority('bizProcessManager:view')")
    @ApiOperation(value = "更新名称和排序")
    @PostMapping("/baseInfo")
    public void update(@RequestBody ActivitiFormUpdateVo updateVo) {
        ActivitiForm activitiForm = new ActivitiForm();
        activitiForm.setId(updateVo.getId());
        activitiForm.setName(updateVo.getName());
        activitiForm.setSort(updateVo.getSort());
        activitiFormService.updateById(activitiForm);
    }

}